(function(root, factory) {
  factory(root.jQuery);
})(window, function($) {
  var __DEF__ = {
    title: "",
    mdmCode: "",
    childDocumentFlag: true,
    effect: 200,
    data: []
  };
  var __PROTOTYPE__ = {
    _init: function() {
      this.addClass("auth-list");
      this.code = '<li>' + '<p class="authitem auth1 pr" data-id="1" style="display:none"><span>'
      if(this.childDocumentFlag){
        this.code +=' <i class="iconfont icon-jiantouxia arrow icon-close"></i>'
      }
      this.code +=this.title +'</span><span>' + this.mdmCode +
        '</span><span></span></p>' +
        '<ul class="">';
      this.code += this._render();
      this.code += '</ul></li>';
      this.html(this.code);
    },
    _render: function() {
      var code = "";
      var that=this;
     // console.log(JSON.stringify(that.data))
      if(that.data.length===0){
        
        code +='<li class="auth-li text-center text-df text-grey padding-top">暂无子设备</li>'
        return code
      }
      $.each(that.data, function(i, data) {
        var _data = data;
        var name= data.deviceName!='null'?data.deviceName:'';
        code += '<li class="auth-li">' +
          '<p class="authitem auth2 pr" data-id="' + data.id + '">' +
          '<span class="btn-toggle" style="padding-left:12px">' 
          if(data.childDocuments&&data.childDocuments.length>0){
             code +='<i class="iconfont icon-jiantouxia arrow"></i>';
          }
           code += name +
          '</span><span>' + this.mdmCode +
        '</span><span class="detail">查看详情></span>';
          
          code +='</p>' +
          '<ul class="' + (data.spread ? "" : "hide") + '">';
          if(data.childDocuments&&data.childDocuments.length>0){
            $.each(data.childDocuments, function(j, data) {
              // console.log(data.childDocuments.length>0)
              var name= data.deviceName!='null'?data.deviceName:'';
              code += '<li>' +
                '<p class="authitem auth3 pr" data-id="' + data.id + '">' +
                '<span class="btn-toggle" style="padding-left:18px">'
                if(data.childDocuments&&data.childDocuments.length>0){
                   code +='<i class="iconfont icon-jiantouxia arrow"></i>';
                }
                code += name +
                '</span><span>' + this.mdmCode +
            '</span><span class="detail">查看详情></span>'
                
                code +='</p>'
                  if(data.childDocuments&&data.childDocuments.length>0){
                    code +='<ul class="' + (data.spread ? "" : "hide") + '">';
                    code += that._childs(data,3);
                    code += '</ul>';
                  }
              code += '</li>';
            });
          }
        
        
        code += '</ul></li>';
      });
      return code;
    },
    _childs:function(data,i){
      var distance=6*(i+1);
      i++;
      var code = "";
      var that=this;
      $.each(data.childDocuments, function(j, data) {
        var name= data.deviceName!='null'?data.deviceName:'';
        code += '<li>' +
            '<p class="authitem auth3 pr" data-id="' + data.id + '">' +
            '<span class="btn-toggle" style="padding-left:'+distance+'px">'
            if(data.childDocuments&&data.childDocuments.length>0){
               code +='<i class="iconfont icon-jiantouxia arrow"></i>';
            }
            code += name  +
            '</span><span>' + this.mdmCode +
        '</span><span class="detail">查看详情></span>'
          code +='</p>';
            if(data.childDocuments&&data.childDocuments.length>0){
              code +='<ul class="' + (data.spread ? "" : "hide") + '">';
              code += that._childs(data,i);
              code += '</ul>';
            }
        code += '</li>';
      });
      return code;
    },
    _handle: function() {
      var $this = this;
      var effect = this.effect;
      this.find(".arrow").on('click', function() {
        $(this).toggleClass("icon-close").parent().parent().next().slideToggle(effect);
      });
      this.find(".detail").on('tap', function() {
        var $chbox = $(this).parent();
        var id=$chbox.attr('data-id')
        console.log(id)
        mui.openWindow({
          url: '../help/filesDetail.html',
          id: '../help/fileChildDetail.html',
          createNew: true, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
          extras: {
            pageId: id
          }
        })
        // if (!$this.bubble) {
        //   var isChecked = $chbox.toggleClass("checked").hasClass("checked");
        //   $chbox.html(isChecked ? "&#x3127;" : "&#x3128;");
        //   if (isChecked) {
        //     $this.find("[phref='" + $chbox.attr('mhref') + "']").addClass('checked').html("&#x3127;");
        //   } else {
        //     $this.find("[phref='" + $chbox.attr('mhref') + "']").removeClass('checked').html("&#x3128;");
        //   }
        // }
        // var $pch = $this.find("[mhref='" + $chbox.attr('phref') + "']");
        // var $siblings = $this.find("[phref='" + $chbox.attr('phref') + "']");
        // var sum = $siblings.size();
        // var curr = $siblings.filter(".checked").size();
        // if (sum == curr) {
        //   $pch.addClass("checked");
        // } else if (curr == 0) {
        //   $pch.removeClass("checked");
        // }
        // $pch.html(sum == curr ? "&#x3127;" : curr == 0 ? "&#x3128;" : "&#x3138;");
        // $this.bubble = true;
        // $pch.trigger('click');
        window.setTimeout(function() {
          // $this.bubble = false;
        }, 200);
      });
    }
  };
  $.fn.groupRule = function(options) {
    this.extend(__DEF__);
    this.extend(options);
    this.extend(__PROTOTYPE__);
    this._init();
    this._handle();
  }
});
